package com.ruoyi.business.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.Region;
import com.ruoyi.business.service.ISysRegionService;
import com.ruoyi.common.utils.poi.ExcelUtil;

import java.util.Arrays;
import java.util.List;

/**
 * sys_regionController
 *
 * @author liutietou
 * @since 1.0 2025-07-20
 */
@RestController
@RequestMapping("/business/region")
public class SysRegionController extends BaseController {
    @Autowired
    private ISysRegionService sysRegionService;

    /**
     * 查询sys_region列表
     */
    @PreAuthorize("@ss.hasPermi('business:region:list')")
    @GetMapping("/list")
    public AjaxResult list(Region region) {
        List<Region> list = sysRegionService.findSysRegionTree(region);
        return AjaxResult.success(list);
    }

    /**
     * 导出sys_region列表
     */
    @PreAuthorize("@ss.hasPermi('business:region:export')")
    @Log(title = "sys_region", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Region region) {
        List<Region> list = sysRegionService.findSysRegionTree(region);
        ExcelUtil<Region> util = new ExcelUtil<>(Region.class);
        return util.exportExcel(list, "region");
    }

    /**
     * 获取sys_region详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:region:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysRegionService.findById(id));
    }

    /**
     * 新增sys_region
     */
    @PreAuthorize("@ss.hasPermi('business:region:add')")
    @Log(title = "sys_region", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Region region) {
        sysRegionService.save(region);
        return AjaxResult.success();
    }

    /**
     * 修改sys_region
     */
    @PreAuthorize("@ss.hasPermi('business:region:edit')")
    @Log(title = "sys_region", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Region region) {
        sysRegionService.save(region);
        return AjaxResult.success();
    }

    /**
     * 删除sys_region
     */
    @PreAuthorize("@ss.hasPermi('business:region:remove')")
    @Log(title = "sys_region", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        sysRegionService.deleteByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }
}
